﻿Public Class frmModel
    Public Sub SelectBrandName()
        ComboBox1.DisplayMember = ("Briand_Name")
        ComboBox1.ValueMember = ("Briand_Id")
        ComboBox1.DataSource = Cmd.ExecuteDataTable("select * from tblBriand where Active = 1")
    End Sub
    Public Sub LoadBriand()
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select * from tblBriand where Active = 1")
    End Sub
    Public Sub LoadModel()
        Me.dgv1.DataSource = Cmd.ExecuteDataTable("select M.Model_Id,B.Briand_Name,M.Model_Name from Model M INNER JOIN tblBriand B on B.Briand_id = M.Briand_id where M.Active = 1")
    End Sub
    Private Sub frmModel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgv)
        UI.FormatDataGrid(dgv1)
        LoadBriand()
        LoadModel()
        SelectBrandName()
    End Sub

    Public Sub New()

        InitializeComponent()

        Me.StartPosition = FormStartPosition.CenterScreen

    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Cmd.Parameters("@Briand_id") = ComboBox1.SelectedValue
        Cmd.Parameters("@Model_Name") = TextBox1.Text
        Cmd.ExecuteNonQuery(" INSERT INTO Model VALUES (@Model_Name,@Briand_id,1)")
        TextBox1.Focus()
        TextBox1.SelectAll()
        MsgBox("Insert Success...!")
        LoadModel()
    End Sub

    Private Sub btnSaves_Click(sender As Object, e As EventArgs) Handles btnSaves.Click
        Cmd.Parameters("@Briand_Name") = txtBriand_Name.Text
        Cmd.ExecuteNonQuery("insert into tblBriand values(@Briand_Name,1)")
        LoadBriand()
    End Sub

    Private Sub btnEdite_Click(sender As Object, e As EventArgs) Handles btnEdite.Click
        Cmd.Parameters("@Briand_id") = txtBriand_Id.Text
        Cmd.Parameters("@Briand_Name") = txtBriand_Name.Text
        Cmd.ExecuteNonQuery("UPDATE tblBriand SET Briand_Name = @Briand_Name WHERE Briand_id=@Briand_id ")
        LoadBriand()
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Cmd.Parameters("@Briand_id") = ComboBox1.SelectedValue
        Cmd.Parameters("@Model_Name") = TextBox1.Text
        Cmd.Parameters("@Model_id") = txtModel_id.Text
        Cmd.ExecuteNonQuery("UPDATE Model SET Briand_id = @Briand_id, Model_Name = @Model_Name WHERE Model_id=@Model_id ")
        LoadModel()
    End Sub

    Private Sub dgv1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv1.CellClick
        If Me.dgv1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgv1.SelectedRows(0)
        Me.ComboBox1.Text = row.Cells("Briand_Name1").Value
        Me.txtModel_id.Text = row.Cells("Model_id").Value
        Me.TextBox1.Text = row.Cells("Model_Name").Value
    End Sub

    Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgv.SelectedRows(0)
        Me.txtBriand_Id.Text = row.Cells("Briand_id").Value
        Me.txtBriand_Name.Text = row.Cells("Briand_Name").Value
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Cmd.Parameters("@Briand_Id") = txtBriand_Id.Text
        Cmd.ExecuteNonQuery("UPDATE tblBriand SET Active = 0 WHERE Briand_Id = @Briand_Id")
        LoadBriand()
    End Sub
    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Cmd.Parameters("@Model_id") = txtModel_id.Text
        Cmd.ExecuteNonQuery("UPDATE Model SET Active = 0 WHERE Model_Id = @Model_Id")
        LoadModel()
    End Sub

    Private Sub dgv1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv1.CellContentClick

    End Sub
End Class